<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>

<script type="text/javascript">

Ext.onReady(function() {
    Ext.QuickTips.init();   
    
    
	var contracts = new Array();
	var contract_numbers = new Array();
    

    var required = '<span style="color:red;font-weight:bold" data-qtip="Required">*</span>';
    
    Ext.define('Contract', { 
        extend: 'Ext.data.Model', 
        fields: ['id','oaStudents','contractId','number',
        'payables','disbursements','method',{
        	name:'payTime',
        	convert: function(v, record) {
        		return v.substring(0,10);
        	}
        }] 
    });
                        
	var contract_store = Ext.create('Ext.data.Store', {
		model: 'Contract', 
		autoLoad: false,
		proxy: { 
            type: 'ajax', 
            url: '../service/contract_all',
            extraParams: {'studentid' : Ext.getDom("studentid").value },
            method: 'POST',
            reader: { 
                type: 'json', 
                root: 'root'
            }
    	}
	});
    
    var multiCombo = Ext.create('Ext.form.field.ComboBox', {
	    multiSelect: true,
	    displayField: 'number',
	    valueField: 'id',
	    store: contract_store,
	    afterLabelTextTpl: required,
		allowBlank: false,
	    fieldLabel:'合同(Contract)',
	    anchor: '95%',
	    listeners: {
	    	select: function(comb, records) {
	    		contracts.length = 0;
	    		
	    		Ext.each(records, function(record) {
	    			contracts.push(record.data.id);
	    		});
	    		
	    	}
	    }
	    
	});
    
    Ext.regModel('Student', { 
        extend: 'Ext.data.Model', 
        fields: ['id','displayName','englishName','school','createTime',
        'grade','age','mother','father','address','phone','emergency',
        'oaContracts','MMobile','MContactType','MContact','MWorking','MPosition',
		'FMobile','FContactType','FContact','FWorking',
        'FPosition','relationship','EPhone','sex',{
        	name: 'birthDate',
        	convert: function(v, record) {
        		return v.substring(0,10);
        	}
        },{
        	name: 'createTime',
        	convert: function(v, record) {
        		return v.substring(0,10);
        	}
        }],
	    proxy: {
	        type: 'ajax',
	        url : '../service/student_find',
	        extraParams: {'studentid' : Ext.getDom("studentid").value },
	        idProperty: 'id',
	        reader: {
	            type: 'json',
	            root: 'student'
	        }
	    }
    });
    
    var students = Ext.ModelMgr.getModel('Student');
    
	
	Ext.define('Contact', { 
        extend: 'Ext.data.Model', 
        idProperty : 'value',
        fields: ['name','value'],
    });
    
	var contact_type_store = Ext.create('Ext.data.Store', {
	    model: 'Contact',
	    data   : [
	        {name : 'QQ',   value: '1'},
	        {name : 'MSN',  value: '2'},
	        {name : 'e-mail', value: '3'}
	    ]
	});
	
    
    var top = Ext.create('Ext.form.Panel',{
        id:'main_form',
        width: '100%',
        frame: true,
        bodyPadding: '5 5 0',
        fieldDefaults: {
            labelAlign: 'top',
            msgTarget: 'side'
        },

        items: [{
            xtype: 'container',
            anchor: '100%',
            layout: 'hbox',
            items:[{
                xtype: 'container',
                flex: 1,
                layout: 'anchor',
                items: [{
                    xtype:'hiddenfield',
                    name: 'student.id',
                    id: 'id'
                },{
                    xtype:'textfield',
                    fieldLabel: '姓名(Name)',
                    afterLabelTextTpl: required,
                    allowBlank: false,
                    name: 'student.displayName',
                    id: 'displayName',
                    anchor:'95%'
                },{
                    xtype:'textfield',
                    fieldLabel: '英文名(English Name)',
                    name: 'student.englishName',
                    id: 'englishName',
                    anchor:'95%'
                }, {
                    xtype: 'datefield',
                    name: 'student.birthDate',
                    id: 'birthDate',
                    format: 'Y-m-d',
                    fieldLabel: '出生日期(Date of Birth)',
                    anchor: '95%'
                }, {
                    xtype: 'textfield',
                    name: 'student.school',
                    id: 'school',
                    fieldLabel: '学校',
                    anchor: '95%'
                },multiCombo]
            },{
                xtype: 'container',
                flex: 1,
                layout: 'anchor',
                items: [{
                	xtype: 'radiogroup',
                    fieldLabel: '性别(Gender)',
                    anchor:'40%',
                    id:'sex',
                    items: [
                        {boxLabel: '男', name: 'student.sex', inputValue: '1', id:'sex_male'},
                        {boxLabel: '女', name: 'student.sex', inputValue: '0', id:'sex_female'}
                    ]
                },{
                    xtype:'textfield',
                    fieldLabel: '年龄(Age)',
                    name: 'student.age',
                    id: 'age',
                    anchor:'95%'
                },{
                    xtype: 'textfield',
                    name: 'student.grade',
                    id: 'grade',
                    fieldLabel: '年级(Grade)',
                    anchor: '95%'
                },{
                    xtype: 'textfield',
                    name: 'student.phone',
                    id: 'phone',
                    fieldLabel: '电话(Tel)',
                    anchor: '95%'
                },{
                    xtype: 'textfield',
                    name: 'student.address',
                    id: 'address',
                    fieldLabel: '地址(Address)',
                    anchor: '95%'
                }]
            }]
        },{
            xtype: 'container',
            anchor: '100%',
            layout: 'hbox',
            items: [{
                xtype: 'container',
                flex: 1,
                layout: 'anchor',
                items: [{ 
	                	xtype:'textfield',
	                    fieldLabel: '母亲姓名(Mother)',
	                    afterLabelTextTpl: required,
	                    allowBlank: false,
	                    name: 'student.mother',
	                    id: 'mother',
	                    anchor:'95%'
                    },{
                    	xtype:'textfield',
                        fieldLabel: '父亲姓名(Father)',
                        afterLabelTextTpl: required,
                        allowBlank: false,
                        name: 'student.father',
                        id: 'father',
                        anchor:'95%'
                    }]
            },{
                xtype: 'container',
                flex: 1,
                layout: 'anchor',
                items: [{ 
	                	xtype:'textfield',
	                    fieldLabel: '手机(Mobile)',
	                    afterLabelTextTpl: required,
	                    allowBlank: false,
	                    name: 'student.MMobile',
	                    id: 'MMobile',
	                    anchor:'95%'
                    },{ 
	                	xtype:'textfield',
	                    fieldLabel: '手机(Mobile)',
	                    afterLabelTextTpl: required,
	                    allowBlank: false,
	                    name: 'student.FMobile',
	                    id: 'FMobile',
	                    anchor:'95%'
                    }]
            },{
                xtype: 'container',
                flex: 1,
                layout: 'anchor',
                items: [{
                		xtype: 'container',
            			anchor: '100%',
            			layout: 'hbox',	
            			items:[{
            				xtype: 'combo',
            				fieldLabel: '联系方式',
							labelWidth:60,
							displayField: 'name',
	    					valueField: 'value',
							width: 60,
							labelSeparator: '',
							editable: false,
							name: 'student.MContactType',
							id: 'MContactType',
							queryMode: 'local',
							store: contact_type_store
            			},{
	            			xtype:'textfield',
		                    fieldLabel: '(Contact):',
		                    afterLabelTextTpl: required,
		                    allowBlank: false,
		                    name: 'student.MContact',
		                    id: 'MContact',
							width: '75%',
							
	                    }]
	                	
                    },{ 
                    	xtype: 'container',
            			anchor: '100%',
            			layout: 'hbox',
            			items:[{
            				xtype: 'combo',
            				fieldLabel: '联系方式',
            				labelWidth:60,
            				displayField: 'name',
	    					valueField: 'value',
							width: 60,
							labelSeparator: '',
							editable: false,
							name: 'student.FContactType',
							id: 'FContactType',
							queryMode: 'local',
							store: contact_type_store
            			},{
            				xtype:'textfield',
	                        fieldLabel: '(Contact):',
	                        afterLabelTextTpl: required,
	                        allowBlank: false,
	                        name: 'student.FContact',
	                        id: 'FContact',
							width: '75%'
            			}]
                    	
                    }]
              }]
        },{
        	xtype: 'container',
            anchor: '100%',
            layout: 'hbox',
            items: [{
            	xtype: 'container',
                flex: 1,
                layout: 'anchor',
                items: [{
                	xtype:'textfield',
                    fieldLabel: '母亲工作单位(Working Place)',
                    afterLabelTextTpl: required,
                    allowBlank: false,
                    name: 'student.MWorking',
                    id: 'MWorking',
                    anchor:'95%'
                },{
                	xtype:'textfield',
                    fieldLabel: '父亲工作单位(Working Place)',
                    afterLabelTextTpl: required,
                    allowBlank: false,
                    name: 'student.FWorking',
                    id: 'FWorking',
                    anchor:'95%'
                }]
            },{
            	xtype: 'container',
                flex: 1,
                layout: 'anchor',
                items: [{
                	xtype:'textfield',
                    fieldLabel: '职位(Position)',
                    afterLabelTextTpl: required,
                    allowBlank: false,
                    name: 'student.MPosition',
                    id: 'MPosition',
                    anchor:'95%'
                },{
                	xtype:'textfield',
                    fieldLabel: '职位(Position)',
                    afterLabelTextTpl: required,
                    allowBlank: false,
                    name: 'student.FPosition',
                    id: 'FPosition',
                    anchor:'95%'
                }]
            }]
        },{
            xtype: 'container',
            anchor: '100%',
            layout: 'hbox',
            items: [{
            	xtype: 'container',
                flex: 1,
                layout: 'anchor',
                items: [{
                	xtype:'textfield',
                    fieldLabel: '紧急联系人(Emergency Contact)',
                    afterLabelTextTpl: required,
                    allowBlank: false,
                    name: 'student.emergency',
                    id: 'emergency',
                    anchor:'95%'
                }]
            },{
            	xtype: 'container',
                flex: 1,
                layout: 'anchor',
					items: [{
					xtype:'textfield',
					fieldLabel: '关系(Relationship)',
					afterLabelTextTpl: required,
					allowBlank: false,
					name: 'student.relationship',
					id: 'relationship',
					anchor:'95%'
				}]
            },{
            	xtype: 'container',
                flex: 1,
                layout: 'anchor',
				items: [{
					xtype:'textfield',
					fieldLabel: '电话(Phone Number)',
					afterLabelTextTpl: required,
					allowBlank: false,
					name: 'student.EPhone',
					id: 'EPhone',
					anchor:'95%'
				}]
            }]
        }],

        buttons: [{
            text: '保存',
            handler: function() {
            	if(this.up('form').getForm().isValid()) {
	                this.up('form').getForm().submit({
	                	url:'../service/student_updatestu',
	                	method: 'POST',
	                	params: {'contracts': contracts},
	        			success:function(form, action) {
	        				Ext.getCmp('edit_win').close();
	        				Ext.Msg.alert('提示',action.result.msg);
	        			},failure:function(form, action) {
	        				Ext.Msg.alert('提示',action.result.msg);
	        			}
	                });
	                
	            }
            }
        },{
            text: '清空',
            handler: function() {
                this.up('form').getForm().reset();
            }
        }]
    });
	
	students.load(Ext.getDom("studentid").value, {
    	success: function(value) {
    		top.loadRecord(value);
    		
    		contract_numbers.length = 0;
    		
			Ext.each(value.data.oaContracts, function(record) {
				contract_numbers.push(record.number);
			});
			multiCombo.setValue(contract_numbers);
			
			top.queryById('FContactType').setValue(contact_type_store.findRecord('value',value.data.FContactType));
			top.queryById('MContactType').setValue(contact_type_store.findRecord('value',value.data.MContactType));
			
			if(value.data.sex == 1) {
				top.queryById('sex').queryById('sex_male').setValue(true);
			} else {
				top.queryById('sex').queryById('sex_female').setValue(true);
			}
    		
    		
    		
    	}
    });
	
    top.render(Ext.getDom("studentform"));

});

</script>
</head>
<body>
	<input type="hidden" id="studentid" value="<s:property value='id'/>"/>
	<div id="studentform"></div>
</body>
</html>